package net.oschina.git.fsppp.common.baserx;

import net.oschina.git.fsppp.exception.P3Exception;
import net.oschina.git.fsppp.common.base.BaseFrame;
import net.oschina.git.fsppp.model.SimpleResponse;
import rx.Subscriber;

/**
 * Created by QiHuang on 2016/10/10.
 */
public abstract class RxSubscribe<T> extends Subscriber<T> {
    public BaseFrame mFrame;

    public RxSubscribe(BaseFrame frame) {
        mFrame = frame;
    }

    @Override
    public void onCompleted() {
    }

    @Override
    public void onError(Throwable e) {
//        if (e instanceof P3Exception || e instanceof NullPointerException) {
            mFrame.showMsg(e.getMessage());
//        }
    }

    @Override
    public void onNext(T t) {
        if (t instanceof SimpleResponse) {
            SimpleResponse response = (SimpleResponse) t;
            if (!response.isStatus()) {
                throw new P3Exception(response.getMsg());
            } else {
                if (response.getEntity() == null) {
                    throw new NullPointerException(response.getMsg());
                }
                _onNext(t);
            }
        } else {
            _onNext(t);
        }
    }


    public abstract void _onNext(T t);

}
